// HSS.Logging.Management.ResultPanel.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       ResultPanel.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Logging.Management
{
	#region Using Directives
	using System;
	using System.ComponentModel;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;
	using HSS.Forms;
	#endregion


	internal sealed class ResultPanel : WizardPanel
	{
		private Label _resultLabel;
		private TextBox _resultTextBox;

		public ResultPanel()
		{
			this.InitializeComponent();
		}

		private void InitializeComponent()
		{
			this._resultTextBox = new System.Windows.Forms.TextBox();
			this._resultLabel = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// _resultTextBox
			// 
			this._resultTextBox.Location = new System.Drawing.Point(0, 0);
			this._resultTextBox.Multiline = true;
			this._resultTextBox.Name = "_resultTextBox";
			this._resultTextBox.ReadOnly = true;
			this._resultTextBox.Size = new System.Drawing.Size(544, 271);
			this._resultTextBox.TabIndex = 100;
			this._resultTextBox.TabStop = false;
			this._resultTextBox.Visible = false;
			// 
			// _resultLabel
			// 
			this._resultLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this._resultLabel.Location = new System.Drawing.Point(0, 0);
			this._resultLabel.Name = "_resultLabel";
			this._resultLabel.Size = new System.Drawing.Size(544, 274);
			this._resultLabel.TabIndex = 101;
			this._resultLabel.Visible = false;
			// 
			// ResultPanel
			// 
			this.Controls.Add(this._resultTextBox);
			this.Controls.Add(this._resultLabel);
			this.Name = "ResultPanel";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		public override bool OnNext()
		{
			return true;
		}

		protected override void OnVisibleChanged(EventArgs arg)
		{
			Exception exception = InstallWizard.Error;
			if (base.Visible)
			{
				base.ParentWizard.FinishButton.Enabled = true;
				base.ParentWizard.FinishButton.Focus();
			}
			else
			{
				base.ParentWizard.FinishButton.Enabled = false;
			}
			if (exception == null)
			{
				base.ParentWizard.DisablePreviousButton();
				if (InstallWizard.Action == WizardAction.AddAllFeatures)
				{
					base.Caption = HSS.Logging.Management.Resources.Result_Description;
					this._resultLabel.Text = HSS.Logging.Management.Resources.Setup_succeeded;
				}
				else
				{
					base.Caption = "Database uninstalled.";
					this._resultLabel.Text = "The HSS Sql Log Database has now been uninstalled.";
				}
				this._resultLabel.Visible = true;
				this._resultTextBox.Text = string.Empty;
				this._resultTextBox.Visible = false;
			}
			else
			{
				SqlExecutionException exception2 = exception as SqlExecutionException;
				StringBuilder builder = new StringBuilder();
				this._resultTextBox.ScrollBars = ScrollBars.Vertical;
				base.Caption = HSS.Logging.Management.Resources.Result_Description_Error;
				builder.Append( HSS.Logging.Management.Resources.Setup_failed);
				if (exception2 != null)
				{
					StringBuilder builder2 = new StringBuilder(exception2.Commands);
					builder2.Replace("\n", "\r\n");
					string cmdErrors = builder2.ToString();

					builder.AppendFormat("{0}\r\n\r\n", exception.Message);
					builder.Append( HSS.Logging.Management.Resources.Failure_details);

					if (!string.IsNullOrEmpty(exception2.Server))
						builder.Append(string.Format( HSS.Logging.Management.Resources.SQLServer, exception2.Server) + "\r\n");
					if (!string.IsNullOrEmpty(exception2.Database))
						builder.Append(string.Format( HSS.Logging.Management.Resources.Database, exception2.Database) + "\r\n");
					if (!string.IsNullOrEmpty(exception2.SqlFile))
						builder.Append(string.Format( HSS.Logging.Management.Resources.SQL_file_loaded, exception2.SqlFile) + "\r\n");
					if (!string.IsNullOrEmpty(cmdErrors))
						builder.Append(string.Format( HSS.Logging.Management.Resources.Commands_failed, cmdErrors) + "\r\n\r\n");
					if (null != exception2.Exception)
						builder.Append(string.Format( HSS.Logging.Management.Resources.SQL_Exception, exception2.Exception.ToString()) + "\r\n\r\n\r\n");
				}
				else
				{
					builder.AppendFormat("{0}\r\n\r\n", exception.Message);
					builder.Append( HSS.Logging.Management.Resources.Failure_details);
					builder.AppendFormat("{0}\r\n", exception);
				}
				this._resultTextBox.Text = builder.ToString();
				this._resultTextBox.SelectionStart = 0;
				this._resultTextBox.SelectionLength = 0;
				this._resultTextBox.Visible = true;
				this._resultLabel.Text = string.Empty;
				this._resultLabel.Visible = false;
			}
		}
	}
}